package com.smart.campus.web.controller.school;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.core.page.TableDataInfo;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.school.domain.ScPaymentBill;
import com.smart.campus.school.domain.request.PaymentBillRequest;
import com.smart.campus.school.service.IScPaymentBillService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.iherus.codegen.qrcode.SimpleQrcodeGenerator;
import org.iherus.codegen.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缴费单Controller
 *
 * @author Zhihe.Li
 * @date 2020-05-13
 */
@Controller
@RequestMapping("/school/bill")
public class ScPaymentBillController extends BaseController {
    private String prefix = "school/bill";

    @Autowired
    private IScPaymentBillService scPaymentBillService;

    @RequiresPermissions("school:bill:view")
    @GetMapping()
    public String bill() {
        return prefix + "/bill";
    }

    /**
     * 查询缴费单列表
     */
    @RequiresPermissions("school:bill:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScPaymentBill scPaymentBill) {
        startPage();
        List<ScPaymentBill> list = scPaymentBillService.selectScPaymentBillList(scPaymentBill);
        return getDataTable(list);
    }

    /**
     * 导出缴费单列表
     */
    @RequiresPermissions("school:bill:export")
    @Log(title = "缴费单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScPaymentBill scPaymentBill) {
        List<ScPaymentBill> list = scPaymentBillService.selectScPaymentBillList(scPaymentBill);
        ExcelUtil<ScPaymentBill> util = new ExcelUtil<ScPaymentBill>(ScPaymentBill.class);
        return util.exportExcel(list, "bill");
    }

    /**
     * 新增缴费单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存缴费单
     */
    @RequiresPermissions("school:bill:add")
    @Log(title = "缴费单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PaymentBillRequest request) {


        return toAjax(scPaymentBillService.insertScPaymentBill(request));
    }

    /**
     * 修改缴费单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ScPaymentBill scPaymentBill = scPaymentBillService.selectScPaymentBillById(id);
        mmap.put("scPaymentBill", scPaymentBill);
        return prefix + "/edit";
    }

    /**
     * 修改保存缴费单
     */
    @RequiresPermissions("school:bill:edit")
    @Log(title = "缴费单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScPaymentBill scPaymentBill) {
        return toAjax(scPaymentBillService.updateScPaymentBill(scPaymentBill));
    }

    /**
     * 删除缴费单
     */
    @RequiresPermissions("school:bill:remove")
    @Log(title = "缴费单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(scPaymentBillService.deleteScPaymentBillByIds(ids));
    }


    /**
     * 去支付
     */
    //@RequiresPermissions("school:bill:remove")
    @GetMapping("/toPay/{id}")
    public String toPay(@PathVariable("id") Long id, ModelMap mmap) {
        ScPaymentBill scPaymentBill = scPaymentBillService.selectScPaymentBillById(id);
        mmap.put("scPaymentBill", scPaymentBill);
        return prefix + "/toPay";
    }

    @GetMapping("/getPayCode")
    public String getPayCode(HttpServletRequest request, HttpServletResponse response,Long id) {
        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/png");
        OutputStream out = null;

        try {

            String content = "https://baike.baidu.com/item/%E5%97%B7%E5%A4%A7%E5%96%B5/19817560?fr=aladdin";
            out = response.getOutputStream();
            new SimpleQrcodeGenerator().generate(content).toStream(out);

        }catch (Exception e){

        }
        finally {
            IOUtils.closeQuietly(out);
        }

        return null;
    }



}
